package com.digitalchina.lxnsy.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.ActivitySignUpListActivity;
import com.digitalchina.lxnsy.activity.old.AddActivityActivity;
import com.digitalchina.lxnsy.api.BusinessActivity;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ActivityListVO;
import com.digitalchina.lxnsy.interfaces.ICallBackActivity;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.ActivityReleaseHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2018/8/1 14:13
 * 修改备注：
 * ================================================
 */
public class ActivityMyReleaseFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, ICallBackActivity {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;

    private final int REQUEST_IS_REFRESH = 99;
    private List<ActivityListVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_activity_manage);
    }

    @Override
    public void initMember() {

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(getActivity(), false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ActivityReleaseHolder(parent, true, ActivityMyReleaseFragment.this);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //GotoUtil.gotoActivity(getActivity(), ActivityDetailActivity.class, "ActivityVO", adapter.getAllData().get(position));
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (getActivity().isFinishing())
                    return;

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_PUBLISHED_ACTIVITY_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PUBLISHED_ACTIVITY_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_CANCEL_ACTIVITY_SUCCESS:
                        showToast("取消成功");
                        onRefresh();
                        break;
                    case MSG.MSG_CANCEL_ACTIVITY_FIELD:
                        showToast(msg.obj.toString());
                        break;

                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBeanRow(object.toString(), "rows", ActivityListVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessActivity.getPublishedActivityAll(getActivity(), page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessActivity.getPublishedActivityAll(getActivity(), page, limit, mHandler);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void manage(int position) {
        ActivityListVO vo = (ActivityListVO) adapter.getAllData().get(position);
        GotoUtil.gotoActivity(getActivity(), ActivitySignUpListActivity.class, "activityId", vo.getId());
    }

    @Override
    public void edit(int position) {
        Intent intent = new Intent(getActivity(), AddActivityActivity.class);
        intent.putExtra("editActivity", (Serializable) adapter.getAllData().get(position));
        startActivityForResult(intent, REQUEST_IS_REFRESH);
    }

    @Override
    public void cancel(int position) {
        ActivityListVO vo = (ActivityListVO) adapter.getAllData().get(position);
        BusinessActivity.cancelActivity(getActivity(), vo.getId(), mHandler);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_IS_REFRESH)
            if (resultCode == RESULT_OK)
                onRefresh();

    }
}
